{
 "metadata": {
  "name": "",
  "signature": "sha256:91397cfdb20467e83f077ca2c20621a1f1538eee38badfe90e6d1375d5f6fe82"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "cd 'C:\\Users\\Yulia\\Desktop\\\u0423\u041c\u0410\u0414_\u0411\u0414\u0417\\generator'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pprint\n",
      "import sys\n",
      "\n",
      "cv_res = {\n",
      "\"positive_positive\": 0,\n",
      "\"positive_negative\": 0,\n",
      "\"negative_positive\": 0,\n",
      "\"negative_negative\": 0,\n",
      "\"contradictory\": 0,\n",
      "}\n",
      "\n",
      "testing = range(2)[-1:]\n",
      "for z in testing:\n",
      "    index = str(z)\n",
      "\n",
      "    q=open(\"train\"+index+\".csv\",\"r\")\n",
      "    train = [ a.strip().split(\",\") for a in q]\n",
      "    plus = [a for a in train if a[-1]==\"positive\"]\n",
      "    minus = [a for a in train if a[-1]==\"negative\"]\n",
      "\n",
      "    #print t\n",
      "    q.close()\n",
      "    w=open(\"test\"+index+\".csv\",\"r\")\n",
      "    unknown = [a.strip().split(\",\") for a in w]\n",
      "    w.close()\n",
      "\n",
      "    #attrib_names = [ 'class','a1','a2','a3','a4','a5','a6' ]\n",
      "    attrib_names = [\n",
      "    'top-left-square',\n",
      "    'top-middle-square',\n",
      "    ' top-right-square',\n",
      "    'middle-left-square',\n",
      "    'middle-middle-square',\n",
      "    'middle-right-square',\n",
      "    'bottom-left-square',\n",
      "    'bottom-middle-square',\n",
      "    'bottom-right-square',\n",
      "    'class'\n",
      "    ]\n",
      "\n",
      "\n",
      "    def make_intent(example):\n",
      "        global attrib_names\n",
      "        return set([i+':'+str(k) for i,k in zip(attrib_names,example)])\n",
      "    \n",
      "    def check_hypothesis(context_plus, context_minus, example):\n",
      "      #  print example\n",
      "        eintent = make_intent(example)\n",
      "      #  print eintent\n",
      "        eintent.discard('class:positive')\n",
      "        eintent.discard('class:negative')\n",
      "        labels = {}\n",
      "        global cv_res\n",
      "        for e in context_plus:\n",
      "            ei = make_intent(e)\n",
      "            candidate_intent = ei & eintent\n",
      "            closure = [ make_intent(i) for i in context_minus if make_intent(i).issuperset(candidate_intent)]\n",
      "            closure_size = len([i for i in closure if len(i)])\n",
      "            #print closure\n",
      "            print closure_size * 1.0 / len(context_minus)\n",
      "            res = reduce(lambda x,y: x&y if x&y else x|y, closure ,set())\n",
      "            for cs in ['positive','negative']:\n",
      "                if 'class:'+cs in res:\n",
      "                    labels[cs] = True\n",
      "                    labels[cs+'_res'] = candidate_intent\n",
      "                    labels[cs+'_total_weight'] = labels.get(cs+'_total_weight',0) +closure_size * 1.0 / len(context_minus) / len(context_plus)\n",
      "        for e in context_minus:\n",
      "            ei = make_intent(e)\n",
      "            candidate_intent = ei & eintent\n",
      "            closure = [ make_intent(i) for i in context_plus if make_intent(i).issuperset(candidate_intent)]\n",
      "            closure_size = len([i for i in closure if len(i)])\n",
      "            #print closure_size * 1.0 / len(context_plus)\n",
      "            res = reduce(lambda x,y: x&y if x&y else x|y, closure, set())\n",
      "            for cs in ['positive','negative']:\n",
      "                if 'class:'+cs in res:\n",
      "                    labels[cs] = True\n",
      "                    labels[cs+'_res'] = candidate_intent\n",
      "                    labels[cs+'_total_weight'] = labels.get(cs+'_total_weight',0) +closure_size * 1.0 / len(context_plus) / len(context_minus)\n",
      "       # print eintent\n",
      "       # print labels\n",
      "        if example[-1] == \"positive\" and labels[\"positive_total_weight\"]>labels[\"negative_total_weight\"]:\n",
      "           cv_res[\"positive_positive\"] += 1\n",
      "        if example[-1] == \"negative\" and labels[\"positive_total_weight\"]>labels[\"negative_total_weight\"]:\n",
      "           cv_res[\"negative_positive\"] += 1\n",
      "        if example[-1] == \"positive\" and labels[\"positive_total_weight\"]<labels[\"negative_total_weight\"]:\n",
      "           cv_res[\"positive_negative\"] += 1\n",
      "        if example[-1] == \"negative\" and labels[\"positive_total_weight\"]<labels[\"negative_total_weight\"]:\n",
      "           cv_res[\"negative_negative\"] += 1\n",
      "\n",
      "    #sanity check:\n",
      "    #check_hypothesis(plus_examples, minus_examples, plus_examples[3])\n",
      "    i = 0\n",
      "    for elem in unknown:\n",
      "        #print elem\n",
      "        #print \"done\"\n",
      "        i += 1\n",
      "        check_hypothesis(plus, minus, elem)\n",
      "    #    if i == 3: break\n",
      "\n",
      "print cv_res\n",
      "results = {'Sensitivity': float(cv_res['positive_positive'])/(cv_res['positive_positive']+cv_res['positive_negative']), 'Specificity': float(cv_res['negative_negative'])/(cv_res['negative_positive']+cv_res['negative_negative']), 'PPV':float(cv_res['positive_positive'])/(cv_res['positive_positive']+cv_res['negative_positive']), 'NPV': float(cv_res['negative_negative'])/(cv_res['negative_negative']+cv_res['negative_positive']), 'FPR': (1-float(cv_res['negative_negative'])/(cv_res['negative_positive']+cv_res['negative_negative'])), 'FDR': (1-float(cv_res['positive_positive'])/(cv_res['positive_positive']+cv_res['negative_positive'])), 'FNR': float(cv_res['positive_negative'])/(cv_res['positive_positive']+cv_res['positive_negative']), 'ACC': float(cv_res['positive_positive']+cv_res['negative_negative'])/(cv_res['positive_positive']+cv_res['positive_negative']+cv_res['negative_positive']+cv_res['negative_negative']), 'F1': float(cv_res['positive_positive']*2)/(cv_res['positive_positive']*2+cv_res['positive_negative']+cv_res['negative_positive'])}\n",
      "print results"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}